//
//  PassengerInfoViewController.m
//  JiongTu
//
//  Created by 杨璟 on 16/5/10.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "PassengerInfoViewController.h"

#import "MyPresentationViewController.h"
#import "MyPresentationAnimationController.h"

#import "JTPassengerTrip.h"
#import "JTDriverTrip.h"
#import "JTUser.h"

#import "JTPushTool.h"

#import "UIImageView+WebCache.h"

#import <AVOSCloud.h>
#import <SVProgressHUD.h>

@interface PassengerInfoViewController ()<UIViewControllerTransitioningDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iconView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *genderLabel;

@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

@property (weak, nonatomic) IBOutlet UILabel *startLabel;

@property (weak, nonatomic) IBOutlet UILabel *destinationLabel;

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UILabel *remarkLabel;

@property (weak, nonatomic) IBOutlet UIButton *refuseBtn;

@property (nonatomic, strong) JTPassengerTrip *theTrip;

@end

@implementation PassengerInfoViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.transitioningDelegate = self;
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        self.transitioningDelegate = self;
    }
    return self;
}

#pragma mark - setter

- (void)setTheTrip:(JTPassengerTrip *)theTrip {
    [self.iconView sd_setImageWithURL:theTrip.passenger.iconPath placeholderImage:[UIImage imageNamed:@"头像占位"]];
    
    self.nameLabel.text = theTrip.passenger.nickname;
    
    if ([theTrip.passenger.gender isEqualToString:@"男"]) {
        self.genderLabel.text = @"先生";
    } else if ([theTrip.passenger.gender isEqualToString:@"女"]) {
        self.genderLabel.text = @"女士";
    }
    
    self.countLabel.text = [NSString stringWithFormat:@"%@次", theTrip.passenger.current];
    
    self.phoneLabel.text = theTrip.passenger.mobilePhoneNumber;
    self.startLabel.text = [theTrip.startLocation stringByAppendingString:theTrip.startStreet];
    self.destinationLabel.text = [theTrip.endLocation stringByAppendingString:theTrip.endStreet];
    self.dateLabel.text = theTrip.time;
    self.timeLabel.text = theTrip.timeInterval;
    
    if ([theTrip.remark isEqualToString:@""]) {
        self.remarkLabel.text = @"无";
    } else {
        self.remarkLabel.text = theTrip.remark;
    }
    
    if ([theTrip.isOrdered isEqualToNumber:@(1)]) {
        self.refuseBtn.hidden = NO;
    } else {
        self.refuseBtn.hidden = YES;
    }
}

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.onePassenger) {
        self.theTrip = self.onePassenger;
    }
}

- (void)viewWillLayoutSubviews {
    self.iconView.layer.cornerRadius = self.iconView.bounds.size.width * 0.5;
    self.iconView.layer.masksToBounds = YES;
}

#pragma mark - 点击事件

- (IBAction)closeBtnClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)refuseClick:(UIButton *)sender {
    NSLog(@"%ld", -[self.onePassenger.orderSeatNumber integerValue]);
    sender.enabled = NO;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"真的要拒绝乘客吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD showWithStatus:@"正在拒绝" maskType:SVProgressHUDMaskTypeBlack];
        [JTPushTool pushToRefusePassenger:self.onePassenger withBlock:^(BOOL succeeded, NSError *error) {
            AVObject *pass = [AVObject objectWithoutDataWithClassName:@"PassengerTrip" objectId:self.onePassenger.objectId];
            [pass setObject:nil forKey:@"DriverInformation"];
            [pass setObject:nil forKey:@"DriverTripInformation"];
            [pass setObject:@(0) forKey:@"IsOrdered"];
            [pass saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    AVObject *driver = [AVObject objectWithoutDataWithClassName:@"DriverTrip" objectId:self.oneDriver.objectId];
                    NSInteger count = [self.onePassenger.orderSeatNumber integerValue];
                    [driver removeObject:self.onePassenger.objectId forKey:@"Passenger"];
                    [driver incrementKey:@"current" byAmount:@(-count)];
                    [driver saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        
                        if (succeeded) {
                            //                        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                            //                        [dict setObject:[JTUser currentUser].nickname forKey:@"userName"];
                            //                        [AVOSCloud requestSmsCodeWithPhoneNumber:self.onePassenger.passenger.mobilePhoneNumber templateName:@"push" variables:dict callback:^(BOOL succeeded, NSError *error) {
                            //                            if (succeeded) {
                            [SVProgressHUD showSuccessWithStatus:@"成功拒绝"];
                            [self dismissViewControllerAnimated:YES completion:self.refuseBlock];
                            //                            }
                            //                        }];
                        }
                        
                    }];
                }
            }];
        }];
    }];
    [alert addAction:cancel];
    [alert addAction:sure];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (IBAction)phoneCall:(id)sender {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",self.phoneLabel.text];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}

#pragma mark - UIViewControllerTransitioningDelegate

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source
{
    if (presented == self) {
        MyPresentationViewController *presen = [[MyPresentationViewController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
        [presen setHidssView:^{
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        return presen;
    }
    return nil;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    if (presented == self) {
        return [[MyPresentationAnimationController alloc] initWithPresenting:true];
    } else {
        return nil;
    }
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    if (dismissed == self) {
        return [[MyPresentationAnimationController alloc] initWithPresenting:false];
    } else {
        return nil;
    }
}

@end
